package com.unittec.zk.provider.exam.controller;

import com.alibaba.fastjson.JSON;
import com.unittec.zk.provider.exam.service.ElQuService;
import com.unittec.zk.provider.exam.service.ElRepoService;
import com.unittec.zk.provider.log.Log;
import com.unittec.zk.provider.utils.ValidationUtils;
import com.unittec.zk.sdk.exam.request.*;
import com.unittec.zk.sdk.exam.response.Res201000;
import com.unittec.zk.sdk.log.enums.LogOperateEnum;
import com.unittec.zk.sdk.log.enums.LogStatusEnum;
import com.unittec.zk.sdk.root.domain.GatewayRequest;
import com.unittec.zk.sdk.root.domain.Result;
import com.unittec.zk.sdk.root.enums.BizCode;
import com.unittec.zk.sdk.root.enums.BizCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Api(tags = "试题管理")
public class QuController {

    @Resource
    private Log log;

    @Resource
    private ElRepoService elRepoService;

    @Resource
    private ElQuService elQuService;

    //题库-列表
    @RequestMapping(value = "/201007", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "题库-列表", notes = "题库-列表")
    public Result<Res201000> getRepoList(@RequestBody GatewayRequest<Req201007> req) {
        log.debug(req, "getRepoList {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elRepoService.getRepoList(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201007", JSON.toJSONString(req), "题库-列表", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getRepoList {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201007", JSON.toJSONString(req), "题库-列表", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201007", JSON.toJSONString(req), "题库-列表", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //试题-分页
    @RequestMapping(value = "/201006", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "试题-分页", notes = "试题-分页")
    public Result<Res201000> getQuPage(@RequestBody GatewayRequest<Req201006> req) {
        log.debug(req, "getQuPage {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elQuService.getQuPage(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201006", JSON.toJSONString(req), "试题-分页", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getQuPage {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201006", JSON.toJSONString(req), "试题-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201006", JSON.toJSONString(req), "试题-分页", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //试题-增/改
    @RequestMapping(value = "/201008", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "试题-增/改", notes = "试题-增/改")
    public Result<Res201000> saveOrUpdateQu(@RequestBody GatewayRequest<Req201008> req) {
        log.debug(req, "saveOrUpdateQu {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elQuService.saveOrUpdateQu(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201008", JSON.toJSONString(req), "试题-增/改", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "saveOrUpdateQu {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201008", JSON.toJSONString(req), "试题-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201008", JSON.toJSONString(req), "试题-增/改", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //试题-查
    @RequestMapping(value = "/201009", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "试题-查", notes = "试题-查")
    public Result<Res201000> getQuById(@RequestBody GatewayRequest<Req201009> req) {
        log.debug(req, "getQuById {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elQuService.getQuById(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201009", JSON.toJSONString(req), "试题-查", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "getQuById {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201009", JSON.toJSONString(req), "试题-查", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201009", JSON.toJSONString(req), "试题-查", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
    
    //试题-删
    @RequestMapping(value = "/201010", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "试题-删", notes = "试题-删")
    public Result<Res201000> delQuByIds(@RequestBody GatewayRequest<Req201010> req) {
        log.debug(req, "delQuByIds {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elQuService.delQuByIds(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201010", JSON.toJSONString(req), "试题-删", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "delQuByIds {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201010", JSON.toJSONString(req), "试题-删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201010", JSON.toJSONString(req), "试题-删", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }

    //试题-批量增加/移出题库
    @RequestMapping(value = "/201011", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "试题-批量增加/移出题库", notes = "试题-批量增加/移出题库")
    public Result<Res201000> batchOperation(@RequestBody GatewayRequest<Req201011> req) {
        log.debug(req, "batchOperation {}  ", "begin");
        long startLogTime = System.currentTimeMillis();
        BizCodeEnum bizCodeEnum = BizCode.ERROR;
        Res201000 res201000 = null;
        try {
            ValidationUtils.validateEntityStr(req.getContent());
            Object o = elQuService.batchOperation(req.getContent());
            res201000 = new Res201000();
            res201000.setObject(o);
            bizCodeEnum = BizCode.SUCCESS;
        } catch (Exception e) {
            log.error(req, e.toString(), e);
            log.addOperateLog("201011", JSON.toJSONString(req), "试题-批量增加/移出题库", log.buildErrmsg(e),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, e.getMessage());
        }
        log.debug(req, "batchOperation {}  ", "end");
        if (bizCodeEnum == BizCode.SUCCESS) {
            log.addOperateLog("201011", JSON.toJSONString(req), "试题-批量增加/移出题库", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.SUCCESS.getCode(), startLogTime);
            return Result.success(req, res201000);
        } else {
            log.addOperateLog("201011", JSON.toJSONString(req), "试题-批量增加/移出题库", bizCodeEnum.getMessage(),
                    LogOperateEnum.LOG_ADD.getCode(), LogStatusEnum.ERROR.getCode(), startLogTime);
            return Result.error(req, bizCodeEnum);
        }
    }
}
